home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4628 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  57 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re:Halp! I don`t know wha
  5. Date: 5 Feb 1996 21:37:35 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4f5tav$etm@gail.ripco.com>
  8. NNTP-Posting-Host: foley.ripco.com
  9.  
  10. etoivane@direct.ca (Ed Toivanen)
  11. in <4eu8sl$f27@aphex.direct.ca> asks:
  12.  
  13. >I've been at this for a couple *days* and can't get it to work.
  14. >It's supposed to graph a function entered at the command line,
  15. >eg. "c:\>graph 2 1" should graph out 2x+1 to stdout using the '*'
  16. >character.  Can anybody help me please?
  17.  
  18. If you get a bunch of stars at strange places (likely with MSDOS),
  19. you were unlucky.  The correct version of "not working" is a segfault,
  20. since you are trashing argv.
  21.  
  22. #if 0
  23.     /* mha - this is the original code */
  24.     for (x = minDomain; x < maxDomain; x++) {
  25.         degree = argc - 1;
  26.         while (degree--) {
  27.             y = ((atol(*++argv)) * (power(x, degree))) + y;
  28.             if (y > maxRange || y < minRange)
  29.                 y = 0;
  30.         }
  31.         lin[y + RCORR][x + DCORR] = '*';
  32.     }
  33. #endif
  34.     /* mha - this is a possible replacement */
  35.     {
  36.         int ncoeff = argc - 1, i;
  37.         double *coeff;
  38.         if (!(coeff = malloc(ncoeff * sizeof(double)))) {
  39.             fprintf(stderr, "Cannot allocate space for coeff\n");
  40.             exit(EXIT_FAILURE);
  41.         }
  42.         for (i = 0; i < ncoeff; i++)
  43.             sscanf(argv[i + 1], "%le", &coeff[i]);
  44.         for (x = minDomain; x < maxDomain; x++) {
  45.             for (y = 0, i = 0; i < ncoeff; i++)
  46.                 y = y * x + coeff[i];
  47.             if (y + RCORR >= 0 && y + RCORR <= RANGE)
  48.                 lin[y + RCORR][x + DCORR] = '*';
  49.         }
  50.         free(coeff);
  51.     }
  52.  
  53.                                                                             
  54. --
  55. * Martin Ambuhl       net: mambuhl@ripco.com
  56. * Chicago, IL (USA)    
  57.